package cn.gov.msa.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.gov.msa.collection.AppLdapService;
import cn.gov.msa.collection.LdapService;
import cn.gov.msa.util.IDCardUtil;
import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.RedisUtils;
import cn.msa.gov.model.UserDetail;
import net.sf.json.JSONObject;

public class LoginInterceptor implements HandlerInterceptor {
	private Logger logger = Logger.getLogger(LoginInterceptor.class);
	private static String loginUrl = PropertiesTools.readProperties("login.url");
	private static String appUrl = PropertiesTools.readProperties("app.login.url");
	boolean flag = false;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		String token = request.getParameter("TOKEN");
		logger.info("token:" + token);
		String redirectUrl = null;
		String userid = null;
		if (null != token) {
			UserDetail userDetail = (UserDetail) RedisUtils.getString(token);
			if (null != userDetail) {
				session.setAttribute("user", userDetail);
				return true;
			} else {
				UserDetail user = AppLdapService.getUserDetail(token, 0);
				logger.info("用户信息：" + JSONObject.fromObject(user));
				if (user.getUserId() != null) {
					RedisUtils.setString(token, user);
					return true;
				}

			}
			redirectUrl = appUrl;
		} else {
			redirectUrl = loginUrl;
			Enumeration<String> en = request.getHeaderNames();
			while (en.hasMoreElements()) {
				String headerName = (String) en.nextElement();
				if ("OAM_REMOTE_USER".equals(headerName)) {
					userid = request.getHeader(headerName);
				}
			}
		}
		logger.info("用户Id：" + userid);
		if (userid != null) {
			session.setAttribute("userid", userid);
			
			if(IDCardUtil.IDCardValidate(userid) == "1"){ //判断是否有效的身份证号码
				UserDetail userDetail4A = null;
				try {
					userDetail4A = (UserDetail) session.getAttribute("user");
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (userDetail4A == null) {
					UserDetail userDetail = LdapService.getUserDetail(userid, 1);
					logger.info("4A用户：" + JSONObject.fromObject(userDetail));
					if (userDetail.getUserId() != null) {
						flag = true;
						session.setAttribute("user", userDetail);
					}
				} else {
					flag = true;
				}
			}
		}
		if (flag) {
			return true;
		} else {
			if (null != token) {
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write("{\"result\":\"-1\",\"info\":\"未登录\"}");
			} else {
				logger.info("跳转地址：" + redirectUrl);
				response.sendRedirect(redirectUrl);//// 上海测试环境
			}
			return true;
		}
//		 return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
	}

}
